<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>visibilitychange事件</title>
</head>

<body>
  <script>
    /**
     * https://developer.mozilla.org/zh-CN/docs/Web/API/Document/visibilitychange_event
     */
    var visibilityChange = '';
    if (typeof document.hidden !== "undefined") {
      visibilityChange = "visibilitychange";
    } else if (typeof document.mozHidden !== "undefined") {
      visibilityChange = "mozvisibilitychange";
    } else if (typeof document.msHidden !== "undefined") {
      visibilityChange = "msvisibilitychange";
    } else if (typeof document.webkitHidden !== "undefined") {
      visibilityChange = "webkitvisibilitychange";
    }
    // 添加监听器
    document.addEventListener(visibilityChange, function () {
      // hidden 失活
      // visible 激活
      console.log('visibilityState', document.visibilityState);
    }, false);
  </script>
</body>

</html>